package com.maila.simplerecord.activity;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.maila.simplerecord.R;
import com.maila.simplerecord.adapter.HackyViewPager;
import com.maila.simplerecord.view.ImageDetailFragment;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by ld_srg on 2016/4/12.
 */
public class ImagePagerActivity extends FragmentActivity {
    private static final String STATE_POSITION = "STATE_POSITION";
    public static final String EXTRA_IMAGE_INDEX = "image_index";
    public static final String EXTRA_IMAGE_URLS = "image_urls";
    public static final String EXTRA_CAREER_TITLE = "career_title";
    private HackyViewPager mPager;
    private int pagerPosition;
    private TextView indicator;
    private String title;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image_detail_pager);

        pagerPosition = getIntent().getIntExtra(EXTRA_IMAGE_INDEX, 0);
        ArrayList<String> urls = new ArrayList<String>();

        List<String> imgpath = (List<String>) getIntent().getSerializableExtra(
                EXTRA_IMAGE_URLS);
        for (int i = 0; i < imgpath.size(); i++) {
                urls.add("file://" + imgpath.get(i));
        }

        title = getIntent().getStringExtra(EXTRA_CAREER_TITLE);
        mPager = (HackyViewPager) findViewById(R.id.pager);
        ImagePagerAdapter mAdapter = new ImagePagerAdapter(getSupportFragmentManager(), urls);
        mPager.setAdapter(mAdapter);
        indicator = (TextView) findViewById(R.id.indicator);
        if (title.length() > 20) {
            indicator.setText(title.substring(0, 20) + "..." );
        } else {
            indicator.setText(title );
        }

        mPager.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageSelected(int arg0) {
                if (title.length() > 20) {
                    indicator.setText(title.substring(0, 20) + "..." );
//                    + "    " + "第" + (arg0 + 1) + "张"
                } else {
                    indicator.setText(title );
                }
            }

        });
        if (savedInstanceState != null) {
            pagerPosition = savedInstanceState.getInt(STATE_POSITION);
        }

        mPager.setCurrentItem(pagerPosition);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putInt(STATE_POSITION, mPager.getCurrentItem());
    }

    private class ImagePagerAdapter extends FragmentStatePagerAdapter {

        public ArrayList<String> fileList;

        public ImagePagerAdapter(FragmentManager fm, ArrayList<String> fileList) {
            super(fm);
            this.fileList = fileList;
        }

        @Override
        public int getCount() {
            return fileList == null ? 0 : fileList.size();
        }

        @Override
        public Fragment getItem(int position) {
            String url = fileList.get(position);
            return ImageDetailFragment.newInstance(url);
        }

    }
}
